import Mock from 'mockjs'
import Cookies from 'js-cookie';

function randomColor () {
  var colorArray = ['#fb0a2a', '#02adea', '#00405d', '#ffcc33'];
  return colorArray[Math.floor(Math.random() * colorArray.length)];
}

function respWapper (data) {
  var resp = {}
  resp['code'] = 0
  resp['data'] = data
  resp['msg'] = ''
  return resp
}

function getRecommend (count, imageSize) {
  return function () {
    var recommends = []
    for (var i = 0; i < count; i++) {
      recommends.push(Mock.mock({
        id: '@id',
        title: '@ctitle(5, 15)',
        imgUrl: Mock.Random.image(imageSize, randomColor()),
        targetUrl: "http://xxxx.com/course/" + Mock.Random.int(1, 100),
        createTime: 1500177620000,
        updateTime: 1500177720000
      }))
    }
    return respWapper(recommends)
  }
}


function getCourses (count, imageSize) {
  return function () {
    var courses = []
    for (var i = 0; i < count; i++) {
      courses.push(Mock.mock({
        id: '@id',
        name: '@ctitle(5, 20)',
        authorId: 0,
        authorName: '@cname',
        authorImgUrl: Mock.Random.image('27x27', randomColor()),
        sourceId: 0,
        sourceName: '@cname',
        briefDesc: '@ctitle(5, 15)',
        detailDesc: '@ctitle(50, 100)',
        orgPrice: Mock.Random.int( 8000, 12000),
        discPrice: Mock.Random.int( 200, 8000),
        imgUrl: Mock.Random.image(imageSize, randomColor()),
        serial: 0,
        hasBuy: 0,
        onlineTime: 1500186620000
      }))
    }
    return respWapper(courses)
  }
}

function getCourseOrders (count, imageSize) {
  return function () {
    var courses = []
    for (var i = 0; i < count; i++) {
      courses.push(Mock.mock({
        courseId: '@id',
        courseName: '@ctitle(5, 15)',
        courseImageUrl: Mock.Random.image(imageSize, randomColor()),
        lastAudioId: '@id',
        lastAudioName: '@ctitle(5, 10)',
        newAudioCount: Mock.Random.int(1, 4)
      }))
    }
    return respWapper(courses)
  }
}

function getCourseDetail () {
  var course = Mock.mock({
        id: '@id',
        name: '@ctitle(5, 20)',
        authorId: 0,
        authorName: '@cname',
        authorImgUrl: Mock.Random.image('27x27', randomColor()),
        sourceId: 0,
        sourceName: '@cname',
        briefDesc: '@ctitle(5, 15)',
        detailDesc: '@ctitle(50, 100)',
        orgPrice: Mock.Random.int( 8000, 120000),
        discPrice: Mock.Random.int( 20, 8000),
        imgUrl: Mock.Random.image('400x400', randomColor()),
        serial: 0,
        hasBuy: 0,
        onlineTime: 1500177620000
  })
  return respWapper(course)
}

function getCourseAudios () {
  var audios = []
  for (var i = 0; i < 10; i++) {
    audios.push(Mock.mock({
      id: '@id',
      name: '@ctitle(5, 20)',
      courseId: '@id',
      serial: 0,
      free: i < 2 ?1:0,
      createTime: 1500177620000,
      updateTime: 1500177720000,
      duration: 12,
      hasBuy:0,
      hasListen: i%2 == 0? 1 : 0
    }))
  }
  return respWapper(audios)
}

function getAudioUrl(){
  return respWapper("http://ostkrhpd0.bkt.clouddn.com/ximalaya.m4a");
}

function loginstatus(){
  return respWapper(Cookies.get('loginStatus'));
}

function login(){
  return respWapper(1); 
}

function logout(){
  return respWapper(1); 
}

function sendcode(){
 return respWapper(1);  
}

export default {
  getRecommend,
  getCourses,
  getCourseDetail,
  getCourseAudios,
  getCourseOrders,
  loginstatus,
  login,
  logout,
  sendcode,
  getAudioUrl
}
